package fid

import (
	"bytes"
	"encoding/json"
	"net/http"
	"os"

	"github.com/labstack/echo"
)

func ResponseError(context echo.Context, err APIError) error {
	retM := map[string]interface{}{
		"code":    err.Code(),
		"message": err.Message(),
		"data":    nil,
	}
	return context.JSON(http.StatusOK, retM)
}

func Response(context echo.Context, data interface{}) error {
	retM := map[string]interface{}{
		"code":    0,
		"message": "",
		"data":    data,
	}
	return context.JSON(http.StatusOK, retM)
}

// for test
func jsonOut(data interface{}) {
	bodyDatas, _ := json.Marshal(data)
	var out bytes.Buffer
	json.Indent(&out, bodyDatas, "", "\t")
	out.WriteTo(os.Stdout)
	os.Stdout.WriteString("\n")
}

func JsonOut(data interface{}) {
	go jsonOut(data)
}
